package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    private WmUserService wmUserService;

    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmUser wmUser) {
        boolean result = wmUserService.save(wmUser);
        if (result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /*根据自媒体用户名查询用户*/
    @Override
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable String name) {
        List<WmUser> list = wmUserService.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));

        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    @PostMapping("/updateById")
    public ResponseResult updateById(@RequestBody WmUser wmUser) {
        boolean result = wmUserService.updateById(wmUser);
        if (result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    @GetMapping("/findOne/{id}")/*根据自媒体文章中userId查询自媒体用户表*/
    public WmUser findWmUserById(@PathVariable Long id) {
        return wmUserService.getById(id);
    }
}
